home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / make-367.lha / make-3.67 / make.info-2 (.txt) < prev    next >
GNU Info File  |  1993-05-16  |  51KB  |  912 lines

  1. This is Info file make.info, produced by Makeinfo-1.54 from the input
  2. file make.texinfo.
  3.    This file documents the GNU Make utility, which determines
  4. automatically which pieces of a large program need to be recompiled,
  5. and issues the commands to recompile them.
  6.    This is Edition 0.42, last updated 14 May 1993, of `The GNU Make
  7. Manual', for `make', Version 3.66 Beta.
  8.    Copyright (C) 1988, '89, '90, '91, '92, '93 Free Software
  9. Foundation, Inc.
  10.    Permission is granted to make and distribute verbatim copies of this
  11. manual provided the copyright notice and this permission notice are
  12. preserved on all copies.
  13.    Permission is granted to copy and distribute modified versions of
  14. this manual under the conditions for verbatim copying, provided also
  15. that the section entitled "GNU General Public License" is included
  16. exactly as in the original, and provided that the entire resulting
  17. derived work is distributed under the terms of a permission notice
  18. identical to this one.
  19.    Permission is granted to copy and distribute translations of this
  20. manual into another language, under the above conditions for modified
  21. versions, except that the text of the translations of the section
  22. entitled "GNU General Public License" must be approved for accuracy by
  23. the Foundation.
  24. File: make.info,  Node: Cleanup,  Prev: Combine By Dependency,  Up: Introduction
  25. Rules for Cleaning the Directory
  26. ================================
  27.    Compiling a program is not the only thing you might want to write
  28. rules for.  Makefiles commonly tell how to do a few other things besides
  29. compiling a program: for example, how to delete all the object files
  30. and executables so that the directory is `clean'.
  31.    Here is how we could write a `make' rule for cleaning our example
  32. editor:
  33.      clean:
  34.              rm edit $(objects)
  35.    In practice, we might want to write the rule in a somewhat more
  36. complicated manner to handle unanticipated situations.  We would do
  37. this:
  38.      .PHONY : clean
  39.      clean :
  40.              -rm edit $(objects)
  41. This prevents `make' from getting confused by an actual file called
  42. `clean' and causes it to continue in spite of errors from `rm'.  (See
  43. *Note Phony Targets::, and *Note Errors in Commands: Errors.)
  44. A rule such as this should not be placed at the beginning of the
  45. makefile, because we do not want it to run by default!  Thus, in the
  46. example makefile, we want the rule for `edit', which recompiles the
  47. editor, to remain the default goal.
  48.    Since `clean' is not a dependency of `edit', this rule will not run
  49. at all if we give the command `make' with no arguments.  In order to
  50. make the rule run, we have to type `make clean'.  *Note How to Run
  51. `make': Running.
  52. File: make.info,  Node: Makefiles,  Next: Rules,  Prev: Introduction,  Up: Top
  53. Writing Makefiles
  54. *****************
  55.    The information that tells `make' how to recompile a system comes
  56. from reading a data base called the "makefile".
  57. * Menu:
  58. * Makefile Contents::           What makefiles contain.
  59. * Makefile Names::              How to name your makefile.
  60. * Include::                     How one makefile can use another makefile.
  61. * MAKEFILES Variable::          The environment can specify extra makefiles.
  62. * Remaking Makefiles::          How makefiles get remade.
  63. * Overriding Makefiles::        How to override part of one makefile
  64.                                  with another makefile.
  65. File: make.info,  Node: Makefile Contents,  Next: Makefile Names,  Up: Makefiles
  66. What Makefiles Contain
  67. ======================
  68.    Makefiles contain five kinds of things: "explicit rules", "implicit
  69. rules", "variable definitions", "directives", and "comments".  Rules,
  70. variables, and directives are described at length in later chapters.
  71.    * An "explicit rule" says when and how to remake one or more files,
  72.      called the rule's targets.  It lists the other files that the
  73.      targets "depend on", and may also give commands to use to create
  74.      or update the targets.  *Note Writing Rules: Rules.
  75.    * An "implicit rule" says when and how to remake a class of files
  76.      based on their names.  It describes how a target may depend on a
  77.      file with a name similar to the target and gives commands to
  78.      create or update such a target.  *Note Using Implicit Rules:
  79.      Implicit Rules.
  80.    * A "variable definition" is a line that specifies a text string
  81.      value for a variable that can be substituted into the text later.
  82.      The simple makefile example shows a variable definition for
  83.      `objects' as a list of all object files (*note Variables Make
  84.      Makefiles Simpler: Variables Simplify.).
  85.    * A "directive" is a command for `make' to do something special while
  86.      reading the makefile.  These include:
  87.         * Reading another makefile (*note Including Other Makefiles:
  88.           Include.).
  89.         * Deciding (based on the values of variables) whether to use or
  90.           ignore a part of the makefile (*note Conditional Parts of
  91.           Makefiles: Conditionals.).
  92.         * Defining a variable from a verbatim string containing
  93.           multiple lines (*note Defining Variables Verbatim: Defining.).
  94.    * `#' in a line of a makefile starts a "comment".  It and the rest of
  95.      the line are ignored, except that a trailing backslash not escaped
  96.      by another backslash will continue the comment across multiple
  97.      lines.  Comments may appear on any of the lines in the makefile,
  98.      except within a `define' directive, and perhaps within commands
  99.      (where the shell decides what is a comment).  A line containing
  100.      just a comment (with perhaps spaces before it) is effectively
  101.      blank, and is ignored.
  102. File: make.info,  Node: Makefile Names,  Next: Include,  Prev: Makefile Contents,  Up: Makefiles
  103. What Name to Give Your Makefile
  104. ===============================
  105.    By default, when `make' looks for the makefile, it tries the
  106. following names, in order: `GNUmakefile', `makefile' and `Makefile'.
  107.    Normally you should call your makefile either `makefile' or
  108. `Makefile'.  (We recommend `Makefile' because it appears prominently
  109. near the beginning of a directory listing, right near other important
  110. files such as `README'.)  The first name checked, `GNUmakefile', is not
  111. recommended for most makefiles.  You should use this name if you have a
  112. makefile that is specific to GNU `make', and will not be understood by
  113. other versions of `make'.  Other `make' programs look for `makefile' and
  114. `Makefile', but not `GNUmakefile'.
  115.    If `make' finds none of these names, it does not use any makefile.
  116. Then you must specify a goal with a command argument, and `make' will
  117. attempt to figure out how to remake it using only its built-in implicit
  118. rules.  *Note Using Implicit Rules: Implicit Rules.
  119.    If you want to use a nonstandard name for your makefile, you can
  120. specify the makefile name with the `-f' or `--file' option.  The
  121. arguments `-f NAME' or `--file NAME' tell `make' to read the file NAME
  122. as the makefile.  If you use more than one `-f' or `--file' option, you
  123. can specify several makefiles.  All the makefiles are effectively
  124. concatenated in the order specified.  The default makefile names
  125. `GNUmakefile', `makefile' and `Makefile' are not checked automatically
  126. if you specify `-f' or `--file'.
  127. File: make.info,  Node: Include,  Next: MAKEFILES Variable,  Prev: Makefile Names,  Up: Makefiles
  128. Including Other Makefiles
  129. =========================
  130.    The `include' directive tells `make' to suspend reading the current
  131. makefile and read one or more other makefiles before continuing.  The
  132. directive is a line in the makefile that looks like this:
  133.      include FILENAMES...
  134. FILENAMES can contain shell file name patterns.
  135.    Extra spaces are allowed and ignored at the beginning of the line,
  136. but a tab is not allowed.  (If the line begins with a tab, it will be
  137. considered a command line.)  Whitespace is required between `include'
  138. and the file names, and between file names; extra whitespace is ignored
  139. there and at the end of the directive.  A comment starting with `#' is
  140. allowed at the end of the line.  If the file names contain any variable
  141. or function references, they are expanded.  *Note How to Use Variables:
  142. Using Variables.
  143.    For example, if you have three `.mk' files, `a.mk', `b.mk', and
  144. `c.mk', and `$(bar)' expands to `bish bash', then the following
  145. expression
  146.      include foo *.mk $(bar)
  147.    is equivalent to
  148.      include foo a.mk b.mk c.mk bish bash
  149.    When `make' processes an `include' directive, it suspends reading of
  150. the containing makefile and reads from each listed file in turn.  When
  151. that is finished, `make' resumes reading the makefile in which the
  152. directive appears.
  153.    One occasion for using `include' directives is when several programs,
  154. handled by individual makefiles in various directories, need to use a
  155. common set of variable definitions (*note Setting Variables: Setting.)
  156. or pattern rules (*note Defining and Redefining Pattern Rules: Pattern
  157. Rules.).
  158.    Another such occasion is when you want to generate dependencies from
  159. source files automatically; the dependencies can be put in a file that
  160. is included by the main makefile.  This practice is generally cleaner
  161. than that of somehow appending the dependencies to the end of the main
  162. makefile as has been traditionally done with other versions of `make'.
  163. *Note Automatic Dependencies::.
  164.    If the specified name does not start with a slash, and the file is
  165. not found in the current directory, several other directories are
  166. searched.  First, any directories you have specified with the `-I' or
  167. `--include-dir' option are searched (*note Summary of Options: Options
  168. Summary.).  Then the following directories (if they exist) are
  169. searched, in this order: `PREFIX/include' (normally
  170. `/usr/local/include') `/usr/gnu/include', `/usr/local/include',
  171. `/usr/include'.
  172.    If an included makefile cannot be found in any of these directories,
  173. a warning message is generated, but it is not an immediately fatal
  174. error; processing of the makefile containing the `include' continues.
  175. Once it has finished reading makefiles, `make' will try to remake any
  176. that are out of date or don't exist.  *Note How Makefiles Are Remade:
  177. Remaking Makefiles.  Only after it has tried to find a way to remake a
  178. makefile and failed, will `make' diagnose the missing makefile as a
  179. fatal error.
  180. File: make.info,  Node: MAKEFILES Variable,  Next: Remaking Makefiles,  Prev: Include,  Up: Makefiles
  181. The Variable `MAKEFILES'
  182. ========================
  183.    If the environment variable `MAKEFILES' is defined, `make' considers
  184. its value as a list of names (separated by whitespace) of additional
  185. makefiles to be read before the others.  This works much like the
  186. `include' directive: various directories are searched for those files
  187. (*note Including Other Makefiles: Include.).  In addition, the default
  188. goal is never taken from one of these makefiles and it is not an error
  189. if the files listed in `MAKEFILES' are not found.
  190.    The main use of `MAKEFILES' is in communication between recursive
  191. invocations of `make' (*note Recursive Use of `make': Recursion.).  It
  192. usually is not desirable to set the environment variable before a
  193. top-level invocation of `make', because it is usually better not to
  194. mess with a makefile from outside.  However, if you are running `make'
  195. without a specific makefile, a makefile in `MAKEFILES' can do useful
  196. things to help the built-in implicit rules work better, such as
  197. defining search paths (*note Directory Search::.).
  198.    Some users are tempted to set `MAKEFILES' in the environment
  199. automatically on login, and program makefiles to expect this to be done.
  200. This is a very bad idea, because such makefiles will fail to work if
  201. run by anyone else.  It is much better to write explicit `include'
  202. directives in the makefiles.  *Note Including Other Makefiles: Include.
  203. File: make.info,  Node: Remaking Makefiles,  Next: Overriding Makefiles,  Prev: MAKEFILES Variable,  Up: Makefiles
  204. How Makefiles Are Remade
  205. ========================
  206.    Sometimes makefiles can be remade from other files, such as RCS or
  207. SCCS files.  If a makefile can be remade from other files, you probably
  208. want `make' to get an up-to-date version of the makefile to read in.
  209.    To this end, after reading in all makefiles, `make' will consider
  210. each as a goal target and attempt to update it.  If a makefile has a
  211. rule which says how to update it (found either in that very makefile or
  212. in another one) or if an implicit rule applies to it (*note Using
  213. Implicit Rules: Implicit Rules.), it will be updated if necessary.
  214. After all makefiles have been checked, if any have actually been
  215. changed, `make' starts with a clean slate and reads all the makefiles
  216. over again.  (It will also attempt to update each of them over again,
  217. but normally this will not change them again, since they are already up
  218. to date.)
  219.    If the makefiles specify a double-colon rule to remake a file with
  220. commands but no dependencies, that file will always be remade (*note
  221. Double-Colon::.).  In the case of makefiles, a makefile that has a
  222. double-colon rule with commands but no dependencies will be remade every
  223. time `make' is run, and then again after `make' starts over and reads
  224. the makefiles in again.  This would cause an infinite loop: `make'
  225. would constantly remake the makefile, and never do anything else.  So,
  226. to avoid this, `make' will *not* attempt to remake makefiles which are
  227. specified as double-colon targets but have no dependencies.
  228.    If you do not specify any makefiles to be read with `-f' or `--file'
  229. options, `make' will try the default makefile names; *note What Name to
  230. Give Your Makefile: Makefile Names..  Unlike makefiles explicitly
  231. requested with `-f' or `--file' options, `make' is not certain that
  232. these makefiles should exist.  However, if a default makefile does not
  233. exist but can be created by running `make' rules, you probably want the
  234. rules to be run so that the makefile can be used.
  235.    Therefore, if none of the default makefiles exists, `make' will try
  236. to make each of them in the same order in which they are searched for
  237. (*note What Name to Give Your Makefile: Makefile Names.) until it
  238. succeeds in making one, or it runs out of names to try.  Note that it
  239. is not an error if `make' cannot find or make any makefile; a makefile
  240. is not always necessary.
  241.    When you use the `-t' or `--touch' option (*note Instead of
  242. Executing the Commands: Instead of Execution.), you would not want to
  243. use an out-of-date makefile to decide which targets to touch.  So the
  244. `-t' option has no effect on updating makefiles; they are really
  245. updated even if `-t' is specified.  Likewise, `-q' (or `--question')
  246. and `-n' (or `--just-print') do not prevent updating of makefiles,
  247. because an out-of-date makefile would result in the wrong output for
  248. other targets.  Thus, `make -f mfile -n foo' will update `mfile', read
  249. it in, and then print the commands to update `foo' and its dependencies
  250. without running them.  The commands printed for `foo' will be those
  251. specified in the updated contents of `mfile'.
  252.    However, on occasion you might actually wish to prevent updating of
  253. even the makefiles.  You can do this by specifying the makefiles as
  254. goals in the command line as well as specifying them as makefiles.
  255. When the makefile name is specified explicitly as a goal, the options
  256. `-t' and so on do apply to them.
  257.    Thus, `make -f mfile -n mfile foo' would read the makefile `mfile',
  258. print the commands needed to update it without actually running them,
  259. and then print the commands needed to update `foo' without running
  260. them.  The commands for `foo' will be those specified by the existing
  261. contents of `mfile'.
  262. File: make.info,  Node: Overriding Makefiles,  Prev: Remaking Makefiles,  Up: Makefiles
  263. Overriding Part of Another Makefile
  264. ===================================
  265.    Sometimes it is useful to have a makefile that is mostly just like
  266. another makefile.  You can often use the `include' directive to include
  267. one in the other, and add more targets or variable definitions.
  268. However, if the two makefiles give different commands for the same
  269. target, `make' will not let you just do this.  But there is another way.
  270.    In the containing makefile (the one that wants to include the other),
  271. you can use the `.DEFAULT' special target to say that to remake any
  272. target that cannot be made from the information in the containing
  273. makefile, `make' should look in another makefile.  *Note Defining
  274. Last-Resort Default Rules: Last Resort, for more information on
  275. `.DEFAULT'.
  276.    For example, if you have a makefile called `Makefile' that says how
  277. to make the target `foo' (and other targets), you can write a makefile
  278. called `GNUmakefile' that contains:
  279.      foo:
  280.              frobnicate > foo
  281.      
  282.      .DEFAULT:
  283.              @$(MAKE) -f Makefile $@
  284.    If you say `make foo', `make' will find `GNUmakefile', read it, and
  285. see that to make `foo', it needs to run the command `frobnicate > foo'.
  286. If you say `make bar', `make' will find no way to make `bar' in
  287. `GNUmakefile', so it will use the commands from `.DEFAULT': `make -f
  288. Makefile bar'.  If `Makefile' provides a rule for updating `bar', `make'
  289. will apply the rule.  And likewise for any other target that
  290. `GNUmakefile' does not say how to make.
  291. File: make.info,  Node: Rules,  Next: Commands,  Prev: Makefiles,  Up: Top
  292. Writing Rules
  293. *************
  294.    A "rule" appears in the makefile and says when and how to remake
  295. certain files, called the rule's "targets" (most often only one per
  296. rule).  It lists the other files that are the "dependencies" of the
  297. target, and "commands" to use to create or update the target.
  298.    The order of rules is not significant, except for determining the
  299. "default goal": the target for `make' to consider, if you do not
  300. otherwise specify one.  The default goal is the target of the first
  301. rule in the first makefile.  If the first rule has multiple targets,
  302. only the first target is taken as the default.  There are two
  303. exceptions: a target starting with a period is not a default unless it
  304. contains one or more slashes, `/', as well; and, a target that defines
  305. a pattern rule has no effect on the default goal.  (*Note Defining and
  306. Redefining Pattern Rules: Pattern Rules.)
  307.    Therefore, we usually write the makefile so that the first rule is
  308. the one for compiling the entire program or all the programs described
  309. by the makefile (often with a target called `all').  *Note Arguments to
  310. Specify the Goals: Goals.
  311. * Menu:
  312. * Rule Example::                An example explained.
  313. * Rule Syntax::                 General syntax explained.
  314. * Wildcards::                   Using wildcard characters such as `*'.
  315. * Directory Search::            Searching other directories for source files.
  316. * Phony Targets::               Using a target that is not a real file's name.
  317. * Force Targets::               You can use a target without commands
  318.                                   or dependencies to mark other
  319.                                   targets as phony.
  320. * Empty Targets::               When only the date matters and the
  321.                                   files are empty.
  322. * Special Targets::             Targets with special built-in meanings.
  323. * Multiple Targets::            When to make use of several targets in a rule.
  324. * Multiple Rules::              How to use several rules with the same target.
  325. * Static Pattern::              Static pattern rules apply to multiple targets
  326.                                   and can vary the dependencies according to
  327.                                   the target name.
  328. * Double-Colon::                How to use a special kind of rule to allow
  329.                                   several independent rules for one target.
  330. * Automatic Dependencies::      How to automatically generate rules giving
  331.                                  dependencies from the source files themselves.
  332. File: make.info,  Node: Rule Example,  Next: Rule Syntax,  Up: Rules
  333. Rule Example
  334. ============
  335.    Here is an example of a rule:
  336.      foo.o : foo.c defs.h       # module for twiddling the frobs
  337.              cc -c -g foo.c
  338.    Its target is `foo.o' and its dependencies are `foo.c' and `defs.h'.
  339. It has one command, which is `cc -c -g foo.c'.  The command line
  340. starts with a tab to identify it as a command.
  341.    This rule says two things:
  342.    * How to decide whether `foo.o' is out of date: it is out of date if
  343.      it does not exist, or if either `foo.c' or `defs.h' is more recent
  344.      than it.
  345.    * How to update the file `foo.o': by running `cc' as stated.  The
  346.      command does not explicitly mention `defs.h', but we presume that
  347.      `foo.c' includes it, and that that is why `defs.h' was added to
  348.      the dependencies.
  349. File: make.info,  Node: Rule Syntax,  Next: Wildcards,  Prev: Rule Example,  Up: Rules
  350. Rule Syntax
  351. ===========
  352.    In general, a rule looks like this:
  353.      TARGETS : DEPENDENCIES
  354.              COMMAND
  355.              ...
  356. or like this:
  357.      TARGETS : DEPENDENCIES ; COMMAND
  358.              COMMAND
  359.              ...
  360.    The TARGETS are file names, separated by spaces.  Wildcard
  361. characters may be used (*note Using Wildcard Characters in File Names:
  362. Wildcards.) and a name of the form `A(M)' represents member M in
  363. archive file A (*note Archive Members as Targets: Archive Members.).
  364. Usually there is only one target per rule, but occasionally there is a
  365. reason to have more (*note Multiple Targets in a Rule: Multiple
  366. Targets.).
  367.    The COMMAND lines start with a tab character.  The first command may
  368. appear on the line after the dependencies, with a tab character, or may
  369. appear on the same line, with a semicolon.  Either way, the effect is
  370. the same.  *Note Writing the Commands in Rules: Commands.
  371.    Because dollar signs are used to start variable references, if you
  372. really want a dollar sign in a rule you must write two of them, `$$'
  373. (*note How to Use Variables: Using Variables.).  You may split a long
  374. line by inserting a backslash followed by a newline, but this is not
  375. required, as `make' places no limit on the length of a line in a
  376. makefile.
  377.    A rule tells `make' two things: when the targets are out of date,
  378. and how to update them when necessary.
  379.    The criterion for being out of date is specified in terms of the
  380. DEPENDENCIES, which consist of file names separated by spaces.
  381. (Wildcards and archive members (*note Archives::.) are allowed here
  382. too.) A target is out of date if it does not exist or if it is older
  383. than any of the dependencies (by comparison of last-modification
  384. times).  The idea is that the contents of the target file are computed
  385. based on information in the dependencies, so if any of the dependencies
  386. changes, the contents of the existing target file are no longer
  387. necessarily valid.
  388.    How to update is specified by COMMANDS.  These are lines to be
  389. executed by the shell (normally `sh'), but with some extra features
  390. (*note Writing the Commands in Rules: Commands.).
  391. File: make.info,  Node: Wildcards,  Next: Directory Search,  Prev: Rule Syntax,  Up: Rules
  392. Using Wildcard Characters in File Names
  393. =======================================
  394.    A single file name can specify many files using "wildcard
  395. characters".  The wildcard characters in `make' are `*', `?' and
  396. `[...]', the same as in the Bourne shell.  For example, `*.c' specifies
  397. a list of all the files (in the working directory) whose names end in
  398. `.c'.
  399.    The character `~' at the beginning of a file name also has special
  400. significance.  If alone, or followed by a slash, it represents your home
  401. directory.  For example `~/bin' expands to `/home/you/bin'.  If the `~'
  402. is followed by a word, the string represents the home directory of the
  403. user named by that word.  For example `~john/bin' expands to
  404. `/home/john/bin'.
  405.    Wildcard expansion happens automatically in targets, in dependencies,
  406. and in commands (where the shell does the expansion).  In other
  407. contexts, wildcard expansion happens only if you request it explicitly
  408. with the `wildcard' function.
  409.    The special significance of a wildcard character can be turned off by
  410. preceding it with a backslash.  Thus, `foo\*bar' would refer to a
  411. specific file whose name consists of `foo', an asterisk, and `bar'.
  412. * Menu:
  413. * Wildcard Examples::           Several examples
  414. * Wildcard Pitfall::            Problems to avoid.
  415. * Wildcard Function::           How to cause wildcard expansion where
  416.                                   it does not normally take place.
  417. File: make.info,  Node: Wildcard Examples,  Next: Wildcard Pitfall,  Up: Wildcards
  418. Wildcard Examples
  419. -----------------
  420.    Wildcards can be used in the commands of a rule, where they are
  421. expanded by the shell.  For example, here is a rule to delete all the
  422. object files:
  423.      clean:
  424.              rm -f *.o
  425.    Wildcards are also useful in the dependencies of a rule.  With the
  426. following rule in the makefile, `make print' will print all the `.c'
  427. files that have changed since the last time you printed them:
  428.      print: *.c
  429.              lpr -p $?
  430.              touch print
  431. This rule uses `print' as an empty target file; see *Note Empty Target
  432. Files to Record Events: Empty Targets.  (The automatic variable `$?' is
  433. used to print only those files that have changed; see *Note Automatic
  434. Variables: Automatic.)
  435.    Wildcard expansion does not happen when you define a variable.
  436. Thus, if you write this:
  437.      objects = *.o
  438. then the value of the variable `objects' is the actual string `*.o'.
  439. However, if you use the value of `objects' in a target, dependency or
  440. command, wildcard expansion will take place at that time.  To set
  441. `objects' to the expansion, instead use:
  442.      objects := $(wildcard *.o)
  443. *Note Wildcard Function::.
  444. File: make.info,  Node: Wildcard Pitfall,  Next: Wildcard Function,  Prev: Wildcard Examples,  Up: Wildcards
  445. Pitfalls of Using Wildcards
  446. ---------------------------
  447.    Now here is an example of a naive way of using wildcard expansion,
  448. that does not do what you would intend.  Suppose you would like to say
  449. that the executable file `foo' is made from all the object files in the
  450. directory, and you write this:
  451.      objects = *.o
  452.      
  453.      foo : $(objects)
  454.              cc -o foo $(CFLAGS) $(objects)
  455. The value of `objects' is the actual string `*.o'.  Wildcard expansion
  456. happens in the rule for `foo', so that each *existing* `.o' file
  457. becomes a dependency of `foo' and will be recompiled if necessary.
  458.    But what if you delete all the `.o' files?  Then `*.o' will expand
  459. into *nothing*.  The target `foo' will have no dependencies and would
  460. be remade by linking no object files.  This is not what you want!
  461.    Actually it is possible to obtain the desired result with wildcard
  462. expansion, but you need more sophisticated techniques, including the
  463. `wildcard' function and string substitution.  *Note The Function
  464. `wildcard': Wildcard Function.
  465. File: make.info,  Node: Wildcard Function,  Prev: Wildcard Pitfall,  Up: Wildcards
  466. The Function `wildcard'
  467. -----------------------
  468.    Wildcard expansion happens automatically in rules.  But wildcard
  469. expansion does not normally take place when a variable is set, or
  470. inside the arguments of a function.  If you want to do wildcard
  471. expansion in such places, you need to use the `wildcard' function, like
  472. this:
  473.      $(wildcard PATTERN)
  474. This string, used anywhere in a makefile, is replaced by a
  475. space-separated list of names of existing files that match the pattern
  476. PATTERN.
  477.    One use of the `wildcard' function is to get a list of all the C
  478. source files in a directory, like this:
  479.      $(wildcard *.c)
  480.    We can change the list of C source files into a list of object files
  481. by replacing the `.o' suffix with `.c' in the result, like this:
  482.      $(patsubst %.c,%.o,$(wildcard *.c))
  483. (Here we have used another function, `patsubst'.  *Note Functions for
  484. String Substitution and Analysis: Text Functions.)
  485.    Thus, a makefile to compile all C source files in the directory and
  486. then link them together could be written as follows:
  487.      objects := $(patsubst %.c,%.o,$(wildcard *.c))
  488.      
  489.      foo : $(objects)
  490.              cc -o foo $(objects)
  491. (This takes advantage of the implicit rule for compiling C programs, so
  492. there is no need to write explicit rules for compiling the files.
  493. *Note The Two Flavors of Variables: Flavors, for an explanation of
  494. `:=', which is a variant of `='.)
  495. File: make.info,  Node: Directory Search,  Next: Phony Targets,  Prev: Wildcards,  Up: Rules
  496. Searching Directories for Dependencies
  497. ======================================
  498.    For large systems, it is often desirable to put sources in a separate
  499. directory from the binaries.  The "directory search" features of `make'
  500. facilitate this by searching several directories automatically to find
  501. a dependency.  When you redistribute the files among directories, you
  502. do not need to change the individual rules, just the search paths.
  503. * Menu:
  504. * General Search::              Specifying a search path that applies
  505.                                   to every dependency.
  506. * Selective Search::            Specifying a search path
  507.                                   for a specified class of names.
  508. * Commands/Search::             How to write shell commands that work together
  509.                                   with search paths.
  510. * Implicit/Search::             How search paths affect implicit rules.
  511. * Libraries/Search::            Directory search for link libraries.
  512. File: make.info,  Node: General Search,  Next: Selective Search,  Up: Directory Search
  513. `VPATH': Search Path for All Dependencies
  514. -----------------------------------------
  515.    The value of the `make' variable `VPATH' specifies a list of
  516. directories that `make' should search.  Most often, the directories are
  517. expected to contain dependency files that are not in the current
  518. directory; however, `VPATH' specifies a search list that `make' applies
  519. for all files, including files which are targets of rules.
  520.    Thus, if a file that is listed as a target or dependency does not
  521. exist in the current directory, `make' searches the directories listed
  522. in `VPATH' for a file with that name.  If a file is found in one of
  523. them, that file becomes the dependency.  Rules may then specify the
  524. names of source files in the dependencies as if they all existed in the
  525. current directory.  *Note Writing Shell Commands with Directory Search:
  526. Commands/Search.
  527.    In the `VPATH' variable, directory names are separated by colons.
  528. The order in which directories are listed is the order followed by
  529. `make' in its search.
  530.    For example,
  531.      VPATH = src:../headers
  532. specifies a path containing two directories, `src' and `../headers',
  533. which `make' searches in that order.
  534.    With this value of `VPATH', the following rule,
  535.      foo.o : foo.c
  536. is interpreted as if it were written like this:
  537.      foo.o : src/foo.c
  538. assuming the file `foo.c' does not exist in the current directory but
  539. is found in the directory `src'.
  540. File: make.info,  Node: Selective Search,  Next: Commands/Search,  Prev: General Search,  Up: Directory Search
  541. The `vpath' Directive
  542. ---------------------
  543.    Similar to the `VPATH' variable but more selective is the `vpath'
  544. directive (note lower case), which allows you to specify a search path
  545. for a particular class of file names, those that match a particular
  546. pattern.  Thus you can supply certain search directories for one class
  547. of file names and other directories (or none) for other file names.
  548.    There are three forms of the `vpath' directive:
  549. `vpath PATTERN DIRECTORIES'
  550.      Specify the search path DIRECTORIES for file names that match
  551.      PATTERN.
  552.      The search path, DIRECTORIES, is a colon-separated list of
  553.      directories to be searched, just like the search path used in the
  554.      `VPATH' variable.
  555. `vpath PATTERN'
  556.      Clear out the search path associated with PATTERN.
  557. `vpath'
  558.      Clear all search paths previously specified with `vpath'
  559.      directives.
  560.    A `vpath' pattern is a string containing a `%' character.  The
  561. string must match the file name of a dependency that is being searched
  562. for, the `%' character matching any sequence of zero or more characters
  563. (as in pattern rules; *note Defining and Redefining Pattern Rules:
  564. Pattern Rules.).  For example, `%.h' matches files that end in `.h'.
  565. (If there is no `%', the pattern must match the dependency exactly,
  566. which is not useful very often.)
  567.    `%' characters in a `vpath' directive's pattern can be quoted with
  568. preceding backslashes (`\').  Backslashes that would otherwise quote
  569. `%' characters can be quoted with more backslashes.  Backslashes that
  570. quote `%' characters or other backslashes are removed from the pattern
  571. before it is compared to file names.  Backslashes that are not in
  572. danger of quoting `%' characters go unmolested.
  573.    When a dependency fails to exist in the current directory, if the
  574. PATTERN in a `vpath' directive matches the name of the dependency file,
  575. then the DIRECTORIES in that directive are searched just like (and
  576. before) the directories in the `VPATH' variable.
  577.    For example,
  578.      vpath %.h ../headers
  579. tells `make' to look for any dependency whose name ends in `.h' in the
  580. directory `../headers' if the file is not found in the current
  581. directory.
  582.    If several `vpath' patterns match the dependency file's name, then
  583. `make' processes each matching `vpath' directive one by one, searching
  584. all the directories mentioned in each directive.  `make' handles
  585. multiple `vpath' directives in the order in which they appear in the
  586. makefile; multiple directives with the same pattern are independent of
  587. each other.
  588.    Thus,
  589.      vpath %.c foo
  590.      vpath %   blish
  591.      vpath %.c bar
  592. will look for a file ending in `.c' in `foo', then `blish', then `bar',
  593. while
  594.      vpath %.c foo:bar
  595.      vpath %   blish
  596. will look for a file ending in `.c' in `foo', then `bar', then `blish'.
  597. File: make.info,  Node: Commands/Search,  Next: Implicit/Search,  Prev: Selective Search,  Up: Directory Search
  598. Writing Shell Commands with Directory Search
  599. --------------------------------------------
  600.    When a dependency is found in another directory through directory
  601. search, this cannot change the commands of the rule; they will execute
  602. as written.  Therefore, you must write the commands with care so that
  603. they will look for the dependency in the directory where `make' finds
  604.    This is done with the "automatic variables" such as `$^' (*note
  605. Automatic Variables: Automatic.).  For instance, the value of `$^' is a
  606. list of all the dependencies of the rule, including the names of the
  607. directories in which they were found, and the value of `$@' is the
  608. target.  Thus:
  609.      foo.o : foo.c
  610.              cc -c $(CFLAGS) $^ -o $@
  611. (The variable `CFLAGS' exists so you can specify flags for C
  612. compilation by implicit rules; we use it here for consistency so it will
  613. affect all C compilations uniformly; *note Variables Used by Implicit
  614. Rules: Implicit Variables..)
  615.    Often the dependencies include header files as well, which you do not
  616. want to mention in the commands.  The automatic variable `$<' is just
  617. the first dependency:
  618.      VPATH = src:../headers
  619.      foo.o : foo.c defs.h hack.h
  620.              cc -c $(CFLAGS) $< -o $@
  621. File: make.info,  Node: Implicit/Search,  Next: Libraries/Search,  Prev: Commands/Search,  Up: Directory Search
  622. Directory Search and Implicit Rules
  623. -----------------------------------
  624.    The search through the directories specified in `VPATH' or with
  625. `vpath' also happens during consideration of implicit rules (*note
  626. Using Implicit Rules: Implicit Rules.).
  627.    For example, when a file `foo.o' has no explicit rule, `make'
  628. considers implicit rules such as to compile `foo.c' if that file
  629. exists.  If such a file is lacking in the current directory, the
  630. appropriate directories are searched for it.  If `foo.c' exists (or is
  631. mentioned in the makefile) in any of the directories, the implicit rule
  632. for C compilation is applied.
  633.    The commands of implicit rules normally use automatic variables as a
  634. matter of necessity; consequently they will use the file names found by
  635. directory search with no extra effort.
  636. File: make.info,  Node: Libraries/Search,  Prev: Implicit/Search,  Up: Directory Search
  637. Directory Search for Link Libraries
  638. -----------------------------------
  639.    Directory search applies in a special way to libraries used with the
  640. linker.  This special feature comes into play when you write a
  641. dependency whose name is of the form `-lNAME'.  (You can tell something
  642. strange is going on here because the dependency is normally the name of
  643. a file, and the *file name* of the library looks like `libNAME.a', not
  644. like `-lNAME'.)
  645.    When a dependency's name has the form `-lNAME', `make' handles it
  646. specially by searching for the file `libNAME.a' in the current
  647. directory, in directories specified by matching `vpath' search paths
  648. and the `VPATH' search path, and then in the directories `/lib',
  649. `/usr/lib', and `PREFIX/lib' (normally `/usr/local/lib').
  650.    For example,
  651.      foo : foo.c -lcurses
  652.              cc $^ -o $@
  653. would cause the command `cc foo.c /usr/lib/libcurses.a -o foo' to be
  654. executed when `foo' is older than `foo.c' or than
  655. `/usr/lib/libcurses.a'.
  656. File: make.info,  Node: Phony Targets,  Next: Force Targets,  Prev: Directory Search,  Up: Rules
  657. Phony Targets
  658. =============
  659.    A phony target is one that is not really the name of a file.  It is
  660. just a name for some commands to be executed when you make an explicit
  661. request.  There are two reasons to use a phony target: to avoid a
  662. conflict with a file of the same name, and to improve performance.
  663.    If you write a rule whose commands will not create the target file,
  664. the commands will be executed every time the target comes up for
  665. remaking.  Here is an example:
  666.      clean:
  667.              rm *.o temp
  668. Because the `rm' command does not create a file named `clean', probably
  669. no such file will ever exist.  Therefore, the `rm' command will be
  670. executed every time you say `make clean'.
  671.    The phony target will cease to work if anything ever does create a
  672. file named `clean' in this directory.  Since it has no dependencies, the
  673. file `clean' would inevitably be considered up to date, and its
  674. commands would not be executed.  To avoid this problem, you can
  675. explicitly declare the target to be phony, using the special target
  676. `.PHONY' (*note Special Built-in Target Names: Special Targets.) as
  677. follows:
  678.      .PHONY : clean
  679. Once this is done, `make clean' will run the commands regardless of
  680. whether there is a file named `clean'.
  681.    Since it knows that phony targets do not name actual files that
  682. could be remade from other files, `make' skips the implicit rule search
  683. for phony targets (*note Implicit Rules::.).  This is why declaring a
  684. target phony is good for performance, even if you are not worried about
  685. the actual file existing.
  686.    Thus, you first write the line that states that `clean' is a phony
  687. target, then you write the rule, like this:
  688.      .PHONY: clean
  689.      clean:
  690.              rm *.o temp
  691.    A phony target should not be a dependency of a real target file; if
  692. it is, its commands are run every time `make' goes to update that file.
  693. As long as a phony target is never a dependency of a real target, the
  694. phony target commands will be executed only when the phony target is a
  695. specified goal (*note Arguments to Specify the Goals: Goals.).
  696.    Phony targets can have dependencies.  When one directory contains
  697. multiple programs, it is most convenient to describe all of the
  698. programs in one makefile `./Makefile'.  Since the target remade by
  699. default will be the first one in the makefile, it is common to make
  700. this a phony target named `all' and give it, as dependencies, all the
  701. individual programs.  For example:
  702.      all : prog1 prog2 prog3
  703.      .PHONY : all
  704.      
  705.      prog1 : prog1.o utils.o
  706.              cc -o prog1 prog1.o utils.o
  707.      
  708.      prog2 : prog2.o
  709.              cc -o prog2 prog2.o
  710.      
  711.      prog3 : prog3.o sort.o utils.o
  712.              cc -o prog3 prog3.o sort.o utils.o
  713. Now you can say just `make' to remake all three programs, or specify as
  714. arguments the ones to remake (as in `make prog1 prog3').
  715.    When one phony target is a dependency of another, it serves as a
  716. subroutine of the other.  For example, here `make cleanall' will delete
  717. the object files, the difference files, and the file `program':
  718.      .PHONY: cleanall cleanobj cleandiff
  719.      
  720.      cleanall : cleanobj cleandiff
  721.              rm program
  722.      
  723.      cleanobj :
  724.              rm *.o
  725.      
  726.      cleandiff :
  727.              rm *.diff
  728. File: make.info,  Node: Force Targets,  Next: Empty Targets,  Prev: Phony Targets,  Up: Rules
  729. Rules without Commands or Dependencies
  730. ======================================
  731.    If a rule has no dependencies or commands, and the target of the rule
  732. is a nonexistent file, then `make' imagines this target to have been
  733. updated whenever its rule is run.  This implies that all targets
  734. depending on this one will always have their commands run.
  735.    An example will illustrate this:
  736.      clean: FORCE
  737.              rm $(objects)
  738.      FORCE:
  739.    Here the target `FORCE' satisfies the special conditions, so the
  740. target `clean' that depends on it is forced to run its commands.  There
  741. is nothing special about the name `FORCE', but that is one name
  742. commonly used this way.
  743.    As you can see, using `FORCE' this way has the same results as using
  744. `.PHONY: clean'.
  745.    Using `.PHONY' is more explicit and more efficient.  However, other
  746. versions of `make' do not support `.PHONY'; thus `FORCE' appears in
  747. many makefiles.  *Note Phony Targets::.
  748. File: make.info,  Node: Empty Targets,  Next: Special Targets,  Prev: Force Targets,  Up: Rules
  749. Empty Target Files to Record Events
  750. ===================================
  751.    The "empty target" is a variant of the phony target; it is used to
  752. hold commands for an action that you request explicitly from time to
  753. time.  Unlike a phony target, this target file can really exist; but
  754. the file's contents do not matter, and usually are empty.
  755.    The purpose of the empty target file is to record, with its
  756. last-modification time, when the rule's commands were last executed.  It
  757. does so because one of the commands is a `touch' command to update the
  758. target file.
  759.    The empty target file must have some dependencies.  When you ask to
  760. remake the empty target, the commands are executed if any dependency is
  761. more recent than the target; in other words, if a dependency has
  762. changed since the last time you remade the target.  Here is an example:
  763.      print: foo.c bar.c
  764.              lpr -p $?
  765.              touch print
  766. With this rule, `make print' will execute the `lpr' command if either
  767. source file has changed since the last `make print'.  The automatic
  768. variable `$?' is used to print only those files that have changed
  769. (*note Automatic Variables: Automatic.).
  770. File: make.info,  Node: Special Targets,  Next: Multiple Targets,  Prev: Empty Targets,  Up: Rules
  771. Special Built-in Target Names
  772. =============================
  773.    Certain names have special meanings if they appear as targets.
  774. `.PHONY'
  775.      The dependencies of the special target `.PHONY' are considered to
  776.      be phony targets.  When it is time to consider such a target,
  777.      `make' will run its commands unconditionally, regardless of
  778.      whether a file with that name exists or what its last-modification
  779.      time is.  *Note Phony Targets: Phony Targets.
  780. `.SUFFIXES'
  781.      The dependencies of the special target `.SUFFIXES' are the list of
  782.      suffixes to be used in checking for suffix rules.  *Note
  783.      Old-Fashioned Suffix Rules: Suffix Rules.
  784. `.DEFAULT'
  785.      The commands specified for `.DEFAULT' are used for any target for
  786.      which no rules are found (either explicit rules or implicit rules).
  787.      *Note Last Resort::.  If `.DEFAULT' commands are specified, every
  788.      file mentioned as a dependency, but not as a target in a rule,
  789.      will have these commands executed on its behalf.  *Note Implicit
  790.      Rule Search Algorithm: Search Algorithm.
  791. `.PRECIOUS'
  792.      The targets which `.PRECIOUS' depends on are given the following
  793.      special treatment: if `make' is killed or interrupted during the
  794.      execution of their commands, the target is not deleted.  *Note
  795.      Interrupting or Killing `make': Interrupts.  Also, if the target
  796.      is an intermediate file, it will not be deleted after it is no
  797.      longer needed, as is normally done.  *Note Chains of Implicit
  798.      Rules: Chained Rules.
  799.      You can also list the target pattern of an implicit rule (such as
  800.      `%.o') as a dependency file of the special target `.PRECIOUS' to
  801.      preserve intermediate files whose target patterns match that file's
  802.      name.
  803. `.IGNORE'
  804.      Simply by being mentioned as a target, `.IGNORE' says to ignore
  805.      errors in execution of commands.  The dependencies and commands for
  806.      `.IGNORE' are not meaningful.
  807.      `.IGNORE' exists for historical compatibility.  Since `.IGNORE'
  808.      affects every command in the makefile, it is not very useful; we
  809.      recommend you use the more selective ways to ignore errors in
  810.      specific commands.  *Note Errors in Commands: Errors.
  811. `.SILENT'
  812.      Simply by being mentioned as a target, `.SILENT' says not to print
  813.      commands before executing them.  The dependencies and commands for
  814.      `.SILENT' are not meaningful.
  815.      `.SILENT' exists for historical compatibility.  We recommend you
  816.      use the more selective ways to silence specific commands.  *Note
  817.      Command Echoing: Echoing.  If you want to silence all commands for
  818.      a particular run of `make', use the `-s' or `--silent' option
  819.      (*note Options Summary::.).
  820. `.EXPORT_ALL_VARIABLES'
  821.      Simply by being mentioned as a target, this tells `make' to export
  822.      all variables to child processes by default.  *Note Communicating
  823.      Variables to a Sub-`make': Variables/Recursion.
  824.    Any defined implicit rule suffix also counts as a special target if
  825. it appears as a target, and so does the concatenation of two suffixes,
  826. such as `.c.o'.  These targets are suffix rules, an obsolete way of
  827. defining implicit rules (but a way still widely used).  In principle,
  828. any target name could be special in this way if you break it in two and
  829. add both pieces to the suffix list.  In practice, suffixes normally
  830. begin with `.', so these special target names also begin with `.'.
  831. *Note Old-Fashioned Suffix Rules: Suffix Rules.
  832. File: make.info,  Node: Multiple Targets,  Next: Multiple Rules,  Prev: Special Targets,  Up: Rules
  833. Multiple Targets in a Rule
  834. ==========================
  835.    A rule with multiple targets is equivalent to writing many rules,
  836. each with one target, and all identical aside from that.  The same
  837. commands apply to all the targets, but their effects may vary because
  838. you can substitute the actual target name into the command using `$@'.
  839. The rule contributes the same dependencies to all the targets also.
  840.    This is useful in two cases.
  841.    * You want just dependencies, no commands.  For example:
  842.           kbd.o command.o files.o: command.h
  843.      gives an additional dependency to each of the three object files
  844.      mentioned.
  845.    * Similar commands work for all the targets.  The commands do not
  846.      need to be absolutely identical, since the automatic variable `$@'
  847.      can be used to substitute the particular target to be remade into
  848.      the commands (*note Automatic Variables: Automatic.).  For example:
  849.           bigoutput littleoutput : text.g
  850.                   generate text.g -$(subst output,,$@) > $@
  851.      is equivalent to
  852.           bigoutput : text.g
  853.                   generate text.g -big > bigoutput
  854.           littleoutput : text.g
  855.                   generate text.g -little > littleoutput
  856.      Here we assume the hypothetical program `generate' makes two types
  857.      of output, one if given `-big' and one if given `-little'.  *Note
  858.      Functions for String Substitution and Analysis: Text Functions,
  859.      for an explanation of the `subst' function.
  860.    Suppose you would like to vary the dependencies according to the
  861. target, much as the variable `$@' allows you to vary the commands.  You
  862. cannot do this with multiple targets in an ordinary rule, but you can
  863. do it with a "static pattern rule".  *Note Static Pattern Rules: Static
  864. Pattern.
  865. File: make.info,  Node: Multiple Rules,  Next: Static Pattern,  Prev: Multiple Targets,  Up: Rules
  866. Multiple Rules for One Target
  867. =============================
  868.    One file can be the target of several rules.  All the dependencies
  869. mentioned in all the rules are merged into one list of dependencies for
  870. the target.  If the target is older than any dependency from any rule,
  871. the commands are executed.
  872.    There can only be one set of commands to be executed for a file.  If
  873. more than one rule gives commands for the same file, `make' uses the
  874. last set given and prints an error message.  (As a special case, if the
  875. file's name begins with a dot, no error message is printed.  This odd
  876. behavior is only for compatibility with other implementations of
  877. `make'.) There is no reason to write your makefiles this way; that is
  878. why `make' gives you an error message.
  879.    An extra rule with just dependencies can be used to give a few extra
  880. dependencies to many files at once.  For example, one usually has a
  881. variable named `objects' containing a list of all the compiler output
  882. files in the system being made.  An easy way to say that all of them
  883. must be recompiled if `config.h' changes is to write the following:
  884.      objects = foo.o bar.o
  885.      foo.o : defs.h
  886.      bar.o : defs.h test.h
  887.      $(objects) : config.h
  888.    This could be inserted or taken out without changing the rules that
  889. really specify how to make the object files, making it a convenient
  890. form to use if you wish to add the additional dependency intermittently.
  891.    Another wrinkle is that the additional dependencies could be
  892. specified with a variable that you set with a command argument to `make'
  893. (*note Overriding Variables: Overriding.).  For example,
  894.      extradeps=
  895.      $(objects) : $(extradeps)
  896. means that the command `make extradeps=foo.h' will consider `foo.h' as
  897. a dependency of each object file, but plain `make' will not.
  898.    If none of the explicit rules for a target has commands, then `make'
  899. searches for an applicable implicit rule to find some commands *note
  900. Using Implicit Rules: Implicit Rules.).
  901. File: make.info,  Node: Static Pattern,  Next: Double-Colon,  Prev: Multiple Rules,  Up: Rules
  902. Static Pattern Rules
  903. ====================
  904.    "Static pattern rules" are rules which specify multiple targets and
  905. construct the dependency names for each target based on the target name.
  906. They are more general than ordinary rules with multiple targets because
  907. the targets do not have to have identical dependencies.  Their
  908. dependencies must be *analogous*, but not necessarily *identical*.
  909. * Menu:
  910. * Static Usage::                The syntax of static pattern rules.
  911. * Static versus Implicit::      When are they better than implicit rules?
  912.